<template>
  <div class="relative flex h-full w-full items-center justify-center">
    <div v-if="structure" class="station-detail">
      <div class="info-wrap">
        <div class="title-wrap">
          <img src="/static/screen/icon-structure-2.png" class="mr-2" />
          <span>监测阵详情</span>
        </div>
        <div class="item-content">
          <div class="item-content_left">
            <div class="item">
              <span>监测阵编号</span>
              <span>{{ structure.monitoringCode }}</span>
            </div>
            <div class="item">
              <span>所属监测网</span>
              <span>{{ structure.netStructureName }}</span>
            </div>
            <div class="item">
              <span>关联项目</span>
              <span>{{ structure.projectName }}</span>
            </div>
            <div class="item">
              <span>经度</span>
              <span>{{ structure.lat }}</span>
            </div>
          </div>
          <div class="item-content_right">
            <div class="item">
              <span>监测阵名称</span>
              <span>{{ structure.structureName }}</span>
            </div>
            <div class="item">
              <span>所属监测区</span>
              <span>{{ structure.regionStructureName }}</span>
            </div>
            <div class="item">
              <span>测点数量</span>
              <span>{{ structure.pointCount }}</span>
            </div>
            <div class="item">
              <span>纬度</span>
              <span>{{ structure.lon }}</span>
            </div>
          </div>
        </div>
      </div>
      <div class="info-wrap">
        <div class="title-wrap">
          <img src="/static/screen/icon-measure.png" class="mr-2" />
          <span>测点列表</span>
          <div class="btn-wrap" @click="toStructure">监测阵大屏</div>
        </div>
        <div
          ref="measureRef"
          class="item-content pointer-events-auto !flex-col !items-start"
        >
          <div class="header left-0 top-0 grid grid-cols-11">
            <div class="col-span-3 text-center">测点名称</div>
            <div class="col-span-2 text-center">测点位置</div>
            <div class="col-span-2 text-center">测点类型</div>
            <div class="col-span-2 text-center">经度</div>
            <div class="col-span-2 text-center">纬度</div>
          </div>
          <div class="w-full flex-1 overflow-y-auto">
            <div
              class="grid w-full grid-cols-11 leading-[32px]"
              v-for="(item, idx) in measure"
              :key="idx"
            >
              <div class="col-span-3 text-center">{{ item.pointName }}</div>
              <div class="col-span-2 text-center">{{ item.location }}</div>
              <div class="col-span-2 text-center">{{ item.pointType }}</div>
              <div class="col-span-2 text-center">{{ item.longitude }}</div>
              <div class="col-span-2 text-center">{{ item.latitude }}</div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="tsx">
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import { getScene2dPreviewDetail, structureDetail } from '../../../../api';
import { useOl } from '../../../olMap/index.vue';

const router = useRouter();

const structure = ref<Record<string, any>>();
const measure = ref([]);

const fetchDetail = (id, data: Record<string, any>) => {
  getScene2dPreviewDetail({ structureId: id }).then((res) => {
    measure.value = res.data;
  });
  structureDetail(id).then((res) => {
    structure.value = {
      ...data,
      ...res.data,
      lat: (+data.lat).toFixed(4),
      lon: (+data.lon).toFixed(4),
    };
  });
};

const { animate } = useOl({
  onClick(res) {
    // 点击监测阵
    if (res.data?.monitoringType === '3') {
      fetchDetail(res.data.id, res.data);
      const { lat, lon } = res.data;
      animate({ center: [lon, lat], zoom: 6 });
    } else {
      structure.value = undefined;
    }
  },
});

const toStructure = () => {
  const { structureId, photoUrl, fileId } = structure.value;
  router.push({
    path: '/screen',
    replace: true,
    query: { active: 1 },
    state: { structureId, photoUrl, fileId },
  });
};

const measureRef = ref();
</script>

<style lang="scss" scoped>
.station-detail {
  position: absolute;
  bottom: 0;
  left: 0;
  display: flex;
  width: 96%;
  height: 215px;
  padding: 16px 10px;
  margin-left: 50%;
  transform: translateX(-50%);
  background: url('/static/screen/structure-detail-bg.png') no-repeat;
  background-size: 100% 100%;

  .info-wrap {
    flex: 1;
    display: flex;
    flex-direction: column;

    .title-wrap {
      position: relative;
      height: 30px;
      margin-bottom: 18px;
      display: flex;
      align-items: center;
      background: url('/static/screen/structure-detail-title-bg.png') no-repeat;
      background-position: left bottom;
      background-size: 68% 5px;

      img {
        margin-left: 12px;
      }

      .btn-wrap {
        position: absolute;
        pointer-events: all;
        cursor: pointer;
        top: 0;
        right: 30px;
        width: 116px;
        height: 30px;
        line-height: 30px;
        text-align: center;
        font-size: 16px;
        color: #fff;
        font-weight: 600;
        background: url('/static/screen/structure-btn-bg.png') no-repeat;
        background-size: cover;
      }
    }

    .item-content {
      flex: 1;
      position: relative;
      overflow: hidden;
      display: flex;
      align-items: center;
      font-size: 12px;

      &_left,
      &_right {
        position: relative;
        flex: 1;
        height: 100%;
        overflow: hidden;
        display: flex;
        flex-direction: column;
        justify-content: space-evenly;

        .item {
          font-size: 14px;
          display: flex;
          justify-content: space-between;
          padding: 0 18px;
        }
      }

      &_left::after {
        content: ' ';
        position: absolute;
        right: 0;
        top: 10px;
        width: 1px;
        height: 128px;
        background: #ffffff;
        opacity: 0.2;
      }

      .header {
        width: calc(100% - 10px);
        height: 32px;
        line-height: 32px;
        background: linear-gradient(
          90deg,
          rgba(49, 131, 255, 0.06) 0%,
          rgba(28, 95, 196, 0.3) 50%,
          rgba(49, 131, 255, 0.06) 100%
        );
        border-radius: 0px 0px 0px 0px;
        border: 1px solid;
        border-image: linear-gradient(
            90deg,
            rgba(92, 174, 249, 0.1),
            rgba(92, 174, 249, 0.8),
            rgba(92, 174, 249, 0.1)
          )
          1 1;
      }
    }
  }
}
</style>
